home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / open / RCS / open.c,v < prev   
Encoding:
Text File  |  1989-06-15  |  5.8 KB  |  299 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     89.06.14.17.14.38;  author ouster;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     89.06.14.15.53.32;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Upgraded to new md organization plus new library.
  27. @
  28. text
  29. @/*
  30.  * open.c --
  31.  *    Test of the open system call.  This does timing tests or
  32.  *    error condition testing.
  33.  */
  34.  
  35. #include <sprite.h>
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <sys/file.h>
  40. #include <sys/stat.h>
  41. #include <option.h>
  42.  
  43. Boolean timing = FALSE;
  44. Boolean errorTest = FALSE;
  45.  
  46. Option optionArray[] = {
  47.     {OPT_TRUE, "t", (char *) &timing, "Do timing test"},
  48.     {OPT_TRUE, "e", (char *) &errorTest, "Do error tests"},
  49. };
  50.  
  51. #define NAME_SIZE    10 * 1024
  52. char name[NAME_SIZE];
  53.  
  54. main(argc, argv)
  55.     int argc;
  56.     char **argv;
  57. {
  58.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 0);
  59.  
  60.     if (timing) {
  61.     DoTiming(argc, argv);
  62.     }
  63.     if (errorTest) {
  64.     DoOpenErrors(argc, argv);
  65.     }
  66. }
  67.  
  68. /*
  69.  * DoTiming --
  70.  *    Time the cost of an open/close pair.
  71.  */
  72. DoTiming(argc, argv)
  73.     int argc;
  74.     char *argv[];
  75. {
  76.     int count, index, i;
  77.     char name[200];
  78.     FILE *f;
  79.     double cost;
  80.     struct timeval before, after;
  81.     struct timezone foo;
  82.  
  83.     if (argc < 2) {
  84.     count = 100;
  85.     } else {
  86.     count = atoi(argv[1]);
  87.     }
  88.     if (argc == 3) {
  89.     sprintf(name, "%s/.FooA", argv[2]);
  90.     }  else {
  91.     strcpy(name, ".FooA");
  92.     }
  93.     index = strlen(name) - 1;
  94.  
  95.     /*
  96.      * Create 20 files in the given directory with names like ".FooA"
  97.      * etc.
  98.      */
  99.  
  100.     for (i = 0; i < 20; i++) {
  101.     name[index] = 'A' + i;
  102.     f = fopen(name, "w");
  103.     if (f == NULL) {
  104.         fprintf(stderr, "Couldn't create %s.\n", name);
  105.         exit(1);
  106.     }
  107.     fclose(f);
  108.     }
  109.  
  110.     /*
  111.      * Time the opens and closes.
  112.      */
  113.  
  114.     gettimeofday(&before, &foo);
  115.     for (i = 0; i < count; i++) {
  116.     name[index] = 'A' + i%20;
  117.     f = fopen(name, "r");
  118.     fclose(f);
  119.     }
  120.     gettimeofday(&after, &foo);
  121.     cost = after.tv_sec - before.tv_sec;
  122.     cost += (after.tv_usec - before.tv_usec)*.000001;
  123.     cost /= count;
  124.     cost *= 1000;
  125.     printf("%d opens and closes at %.2f msec each.\n", count, cost);
  126.  
  127.     /*
  128.      * Remove all the temporary files that were created.
  129.      */
  130.  
  131.     for (count = 0; count < 20; count++) {
  132.     name[index] = 'A' + count;
  133.     unlink(name);
  134.     }
  135. }
  136.  
  137. /*
  138.  * DoOpenErrors --
  139.  *    Attempt to generate all errors associated with open.
  140.  */
  141. DoOpenErrors(argc, argv)
  142.     int argc;
  143.     char *argv[];
  144. {
  145.     printf("Open Error Tests\n");
  146.     printf("    ERROR: indicates failure case did not work\n");
  147.     printf("    otherwise perror() is used to print system error\n\n");
  148.     /*
  149.      * Test bad pathname argument.
  150.      */
  151.     {
  152.     int fd;
  153.     struct stat statb;
  154.  
  155.     if ((fd = open(-1, 0, 0)) >= 0) {
  156.         printf("ERROR: open(-1, 0, 0) succeeded!\n");
  157.         if (fstat(fd, &statb) < 0) {
  158.         perror("but fstat failed");
  159.         } else {
  160.         printf("-1 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  161.         close(fd);
  162.         }
  163.     } else {
  164.         perror("open(-1, 0, 0) (bad pathname)");
  165.     }
  166.     if ((fd = open(15, 0, 0)) >= 0) {
  167.         printf("ERROR: open(15, 0, 0) succeeded!\n");
  168.         if (fstat(fd, &statb) < 0) {
  169.         perror("but fstat failed");
  170.         } else {
  171.         printf("15 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  172.         close(fd);
  173.         }
  174.     } else {
  175.         perror("open(15, 0, 0) (bad pathname)");
  176.     }
  177.     }
  178.     /*
  179.      * Test too long a pathname.
  180.      */
  181.     {
  182.     register char *cPtr;
  183.     register int i;
  184.  
  185.     for (cPtr = name, i=0 ; i < NAME_SIZE ; ) {
  186.         *cPtr++ = 'a'; i++ ;
  187.         *cPtr++ = 'a'; i++ ;
  188.         *cPtr++ = 'a'; i++ ;
  189.         *cPtr++ = 'a'; i++ ;
  190.         *cPtr++ = 'a'; i++ ;
  191.         *cPtr++ = 'a'; i++ ;
  192.         *cPtr++ = 'a'; i++ ;
  193.         *cPtr++ = 'a'; i++ ;
  194.         *cPtr++ = 'a'; i++ ;
  195.         *cPtr++ = 'a'; i++ ;
  196.     }
  197.     if (open(name, 0, 0) >= 0) {
  198.         printf("ERROR: open(tooLongName, 0, 0) succeeded!\n");
  199.     } else {
  200.         perror("open(tooLongName, 0, 0)");
  201.     }
  202.     }
  203.     /*
  204.      *  Do various permission checks.  This checks against violations
  205.      *    of the following conditions:
  206.      *        Exclusive open
  207.      *        read permission
  208.      *        write permission
  209.      *        busy file
  210.      *        writing a directory
  211.      */
  212.     {
  213.     char *name = "./FooA";
  214.     int fd, fd2;
  215.  
  216.     /*
  217.      * Test open-unlink-close
  218.      */
  219.     if ((fd = open(name, O_CREAT, 0444)) < 0) {
  220.         perror("Can't create ./FooA");
  221.     } else {
  222.         if ((fd2 = open(name, O_RDONLY)) >= 0) {
  223.         close(fd2);
  224.         } else {
  225.         perror("Can't open readable file");
  226.         }
  227.         unlink(name);
  228.         close(fd);
  229.     }
  230.     /*
  231.      * Test permission checking.
  232.      */
  233.     if ((fd = open(name, O_CREAT, 0)) < 0) {
  234.         perror("Can't create ./FooA");
  235.     } else {
  236.         if ((fd2 = open(name, O_CREAT|O_EXCL, 0)) >= 0) {
  237.         printf("ERROR: exclusive open of existing file succeeded!\n");
  238.         if (chmod(name, 0) < 0) {
  239.             perror("Can't chmod file");
  240.         }
  241.         close(fd2);
  242.         } else {
  243.         perror("open exclusive");
  244.         }
  245.         if (open(name, O_RDONLY) >= 0) {
  246.         printf("ERROR: opened with no read permission!\n");
  247.         } else {
  248.         perror("read when mode == 0");
  249.         }
  250.         chmod(name, 0444);
  251.         if (open(name, O_WRONLY) >= 0) {
  252.         printf("ERROR: opened with no write permission!\n");
  253.         } else {
  254.         perror("write when mode == read only");
  255.         }
  256.         if (open("/sprite/cmds/csh", O_WRONLY) >= 0) {
  257.         printf("ERROR: opened executing program (csh) for writing!\n");
  258.         } else {
  259.         perror("open active file");
  260.         }
  261.         if (open(".", O_WRONLY) >= 0) {
  262.         printf("ERROR: opened directory for writing!\n");
  263.         } else {
  264.         perror("write directory");
  265.         }
  266.         if (unlink(name) < 0) {
  267.         perror("Can't remove test file");
  268.         }
  269.         /*
  270.          * Test closing closed file.
  271.          */
  272.         close(fd);
  273.         if (close(fd) < 0)  {
  274.         perror("close of closed file");
  275.         } else {
  276.         printf("ERROR: closed a closed file descriptor!\n");
  277.         }
  278.     }
  279.     }
  280. }
  281. @
  282.  
  283.  
  284. 1.1
  285. log
  286. @Initial revision
  287. @
  288. text
  289. @d6 2
  290. d13 4
  291. d18 4
  292. a21 2
  293. extern timing;
  294. extern errorTest;
  295. d30 1
  296. a30 1
  297.     GetOptions(&argc, argv);
  298. @
  299.